package com.itheima.d4_collector;

import java.util.*;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test {
    public static void main(String[] args) {
        Collection<String> list = new ArrayList<>();
        Collections.addAll(list,"张无忌","张三丰","周芷若","赵敏");
        System.out.println(list);
        List<String> list1 = list.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.startsWith("张");
            }
        }).collect(Collectors.toList());
        System.out.println(list1);
        // 注意： 流一旦遇到结束方法，就没了，要用的话，需要再开流；
        Set<String> set = list.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.startsWith("张");
            }
        }).collect(Collectors.toSet());
        System.out.println(set);
       // 收集到数组里
        String[] array = list.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.startsWith("张");
            }
        }).toArray(new IntFunction<String[]>() {
            @Override
            public String[] apply(int value) {
                return new String[value];
            }
        });
        //list.stream().filter(new Predicate<String>() {
        //            @Override
        //            public boolean test(String s) {
        //                return s.startsWith("张");
        //            }
        //        }).toArray();
        System.out.println(array);
    }
}
